import { Component, OnInit, ViewChild } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';

@Component({
  selector: 'app-archive-revert-edit',
  templateUrl: './edit.component.html',
})
export class ArchiveRevertEditComponent implements OnInit {
  record: any = {};
  i: any = {};
  schema: SFSchema = {
    properties: {
      code: { type: 'string', title: '文号' },
      title: { type: 'string', title: '文件标题' },
      returner: { type: 'string', title: '归还者', maxLength: 15 },
      returnAt: { type: 'string', title: '归还时间' },
      // href: { type: 'string', title: '链接', format: 'uri' },
      // description: { type: 'string', title: '描述', maxLength: 140 },
    },
    required: ['returner', 'returnAt'],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $code: {
      widget: 'text'
    },
    $title: {
      widget: 'text'
    },
    $returner: {
      widget: 'string',
    },
    $returnAt: {
      widget: 'date',
      // grid: { span: 24 },
    },
  };

  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
  ) { }

  ngOnInit(): void {
    if (this.record.id > 0) {
      this.http.get(`/borrow/record/${this.record.id}`).subscribe((res:any) => {
        if(!res || !res.id){
          return
        }
        this.i = res;
        this.i.returnAt = new Date();
        this.i.returner = res.lender;

        // this.i.id = res.id;
        this.i.status = '在库';
        // this.i.archiveId = this.record.id;
        // this.i.code = this.record.code;
        // this.i.title = this.record.title;
      });
    }
    // this.http.get(`/user/${this.record.id}`).subscribe(res => (this.i = res));
  }

  save(value: any) {
    this.http.post(`/borrow/save/${this.record.id}`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  close() {
    this.modal.destroy();
  }
}
